package com.dewod.producesales.utils.common;

import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;



import java.util.Locale;

/**
 * description
 *
 * @author guoyang
 */
public class TtsManager {


        private static TtsManager instance;
        private TextToSpeech tts;

        private TtsManager() {}

        public static synchronized TtsManager getInstance() {
            if (instance == null) {
                instance = new TtsManager();
            }
            return instance;
        }

        public void init(Context context) {
            if (context == null) return;

            if (tts == null) {
                tts = new TextToSpeech(context, status -> {
                    if (status == TextToSpeech.SUCCESS) {
                        tts.setLanguage(Locale.CHINESE);
                    }
                });
            }
        }

        public void speakText(String text) {
            if (tts != null && !TextUtils.isEmpty(text)) {
                tts.speak(text, TextToSpeech.QUEUE_FLUSH, null, "tts");
            }
        }

        public void release() {
            if (tts != null) {
                tts.stop();
                tts.shutdown();
                tts = null;
            }
        }



}

